package demo;

import pojo.Province;
import pojo.ResultVO;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * 02-jackson基本使用
 * ②将json字符串转换为javabean对象
 */
public class Demo02 {

    public static void main(String[] args) throws IOException {
        String jsonStr = "{\"provinceId\":1,\"provinceName\":\"湖北省\",\"cityList\":[{\"cityid\":11,\"cityName\":\"武汉市\"},{\"cityid\":12,\"cityName\":\"孝感市\"}]}";
        //将json字符串javabean对象
        Province province = new ObjectMapper().readValue(jsonStr, Province.class);
        System.out.println("province = " + province);

        //将json字符串转换为List对象
        String jsonStr2 = "[{\"provinceId\":1,\"provinceName\":\"湖北省\",\"cityList\":[{\"cityid\":11,\"cityName\":\"武汉市\"},{\"cityid\":12,\"cityName\":\"孝感市\"}]},{\"provinceId\":2,\"provinceName\":\"湖南省\",\"cityList\":[{\"cityid\":21,\"cityName\":\"长沙市\"},{\"cityid\":22,\"cityName\":\"株洲市\"}]}]\n";
        List<Province> list = new ObjectMapper().readValue(jsonStr2, List.class);
        System.out.println("list = " + list);

        //将json字符串转换为map对象
        String jsonStr3 = "{\"msg\":\"添加用户成功!\",\"flag\":true,\"data\":null}\n";
        Map map = new ObjectMapper().readValue(jsonStr3, Map.class);
        System.out.println("map = " + map);

        //将json字符串转换为ResultVO对象
        String jsonStr4 = "{\"flag\":true,\"msg\":\"查询省份成功!\",\"data\":[{\"provinceId\":1,\"provinceName\":\"湖北省\",\"cityList\":[{\"cityid\":11,\"cityName\":\"武汉市\"},{\"cityid\":12,\"cityName\":\"孝感市\"}]},{\"provinceId\":2,\"provinceName\":\"湖南省\",\"cityList\":[{\"cityid\":21,\"cityName\":\"长沙市\"},{\"cityid\":22,\"cityName\":\"株洲市\"}]}]}\n";
        ResultVO resultVO = new ObjectMapper().readValue(jsonStr4, ResultVO.class);
        System.out.println("resultVO = " + resultVO);
    }

}
